package com.huika.renrench.control.more.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response.Listener;
import com.google.gson.reflect.TypeToken;
import com.huika.lib.utils.StringTool;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.UserModel;
import com.huika.renrench.utils.PhoneUtil;
import com.huika.renrench.view.TitleBarHelper;

/**
 * @author yangyuan
 * @date：2015年6月2日,
 * @describe:意见反馈
 */
public class FeedBackActivity extends BaseAct{
	
	private EditText edit_feed_back; 
	private Button button_feed_back;
	private EditText editText_phone; 
	private TextView text_num;
	
	private String userId = "";
	private String Phone = "";
	private UserModel userBean;
	
	@Override
	public void setRootView() {
		setContentView(R.layout.feed_back_activity);
	}
	
	@Override
	protected void initWidget() {
		super.initWidget();
		new TitleBarHelper(this, R.string.common_index_my,-1,R.string.feed_back);//设置头部title
		
		edit_feed_back = (EditText) findViewById(R.id.edit_feed_back);
		editText_phone = (EditText) findViewById(R.id.editText_phone);
		button_feed_back = (Button) findViewById(R.id.button_feed_back);
		text_num = (TextView) findViewById(R.id.text_num);
	
		button_feed_back.setOnClickListener(this);
		
		edit_feed_back.addTextChangedListener(new MyTextChanged());
		
		userBean = GlobalApp.getInstance().getUserModel();
		if (userBean==null) {
			userId = "0";
			Phone = "";
		}else {
			userId = userBean.getUserId();
			Phone = userBean.getPhone();
		}
	}
	
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
		case R.id.button_feed_back:
			if (StringTool.isEmpty(getInputStr(edit_feed_back))) {
				showToastMsg(getResources().getString(R.string.button_feed_back));
				return;
			}
			if (!StringTool.isEmpty(getInputStr(editText_phone)) && !PhoneUtil.IsPhoneStr(getInputStr(editText_phone))) {
				showToastMsg("请输入正确的手机号");
				return;
			} 
			executeUpdateBackgInfoList();
			showLoadingDialog(" ");
			break;
		}
	}
	
	/** 提交意见反馈*/
	private void executeUpdateBackgInfoList(){
		FormResultRequest<String> request = new FormResultRequest<String>(UrlConstants.PUBLISHFEEDBACK, getUpdateBackgResp, this, new TypeToken<RequestResult<String>>() {}.getType());
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();//
		ajaxParams.putCommonTypeParam("userId", userId);//userId
		ajaxParams.putCommonTypeParam("phone", getInputStr(editText_phone));//电话，可以为空
		ajaxParams.putCommonTypeParam("remark", getInputStr(edit_feed_back));//userId
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}
	
	private Listener<RequestResult<String>> getUpdateBackgResp=new Listener<RequestResult<String>>() {
		
		@Override
		public void onResponse(RequestResult<String> result) {
			
			if (result.flag == 1) {
				showToastMsg(result.msg);
				edit_feed_back.setText("");
				finish();
			} else {
				showToastMsg(result.msg);
			}
			dismissLoadingDialog();
			if (mOverlayLayout != null)
				hideOverLay();
		}
	};
	
	private class MyTextChanged implements TextWatcher{

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			text_num.setText((250 - edit_feed_back.getText().toString().length())+"");
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
		
	}

}
